﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace SPTotalAutomationToolkit.SP2013IntranetProjectTemplate.Wizard
{
    public partial class Page2a : UserControl
    {
        private WizardWindow mainWindow;

        internal Page2a(WizardWindow mainWindow)
        {
            this.mainWindow = mainWindow;
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //pbscIntranetHostHeaderForDevTextBox.Text = mainWindow.PresentationModel.SuggestedWebAppUrlForDev;
        }

        private void pbscIntranetHostHeaderForDevTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
            base.OnPreviewKeyDown(e);
        }

        private void pbscIntranetHostHeaderForDevTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // Remove any spaces that may have been pasted in
            pbscIntranetHostHeaderForDevTextBox.Text = pbscIntranetHostHeaderForDevTextBox.Text.Replace(" ","");

            var hostHeader = pbscIntranetHostHeaderForDevTextBox.Text;
            var url = EnsureTrailingSlash("http://" + hostHeader + ":" + pbscIntranetPortForDevTextBox.Text);

            // Update the label on the wizard UI to display the url
            pbscIntranetUrlForDevLabel.Content = url;

            mainWindow.PresentationModel.IntranetWebAppHostHeaderForDev = hostHeader;
            mainWindow.PresentationModel.SiteUrlForDebugging = url;

            ValidateForm();
        }

        private void pbscIntranetPortForDevTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
            base.OnPreviewKeyDown(e);
        }

        private void pbscIntranetPortForDevTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // Remove any spaces that may have been pasted in
            pbscIntranetPortForDevTextBox.Text = pbscIntranetPortForDevTextBox.Text.Replace(" ", "");

            var port = pbscIntranetPortForDevTextBox.Text;
            var url = EnsureTrailingSlash("http://" + pbscIntranetHostHeaderForDevTextBox.Text + ":" + port);

            // Update the label on the wizard UI to display the url
            pbscIntranetUrlForDevLabel.Content = url;

            mainWindow.PresentationModel.IntranetWebAppPortForDev = port;

            ValidateForm();
        }

        private void pbscPeopleHostHeaderForDevTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
            base.OnPreviewKeyDown(e);
        }

        // Prevent users from finishing the wizard if the URL is not formatted correctly.
        private void pbscPeopleHostHeaderForDevTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // Remove any spaces that may have been pasted in
            pbscPeopleHostHeaderForDevTextBox.Text = pbscPeopleHostHeaderForDevTextBox.Text.Replace(" ", "");

            var hostHeader = pbscPeopleHostHeaderForDevTextBox.Text;
            var url = EnsureTrailingSlash("http://" + hostHeader + ":" + pbscPeoplePortForDevTextBox.Text);

            // Update the label on the wizard UI to display the url
            pbscPeopleUrlForDevLabel.Content = url;

            mainWindow.PresentationModel.PeopleWebAppHostHeaderForDev = hostHeader;

            ValidateForm();
        }

        private void pbscPeoplePortForDevTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
            base.OnPreviewKeyDown(e);
        }

        private void pbscPeoplePortForDevTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // Remove any spaces that may have been pasted in
            pbscPeoplePortForDevTextBox.Text = pbscPeoplePortForDevTextBox.Text.Replace(" ", "");

            var port = pbscPeoplePortForDevTextBox.Text;
            var url = EnsureTrailingSlash("http://" + pbscPeopleHostHeaderForDevTextBox.Text + ":" + port);

            // Update the label on the wizard UI to display the url
            pbscPeopleUrlForDevLabel.Content = url;

            mainWindow.PresentationModel.PeopleWebAppPortForDev = port;

            ValidateForm();
        }

        private string EnsureTrailingSlash(string url)
        {
            if (!String.IsNullOrEmpty(url)
                && url[url.Length - 1] != '/')
            {
                url += '/';
            }
            return url;
        }

        private void WriteIntranetHostFile_Checked(object sender, RoutedEventArgs e)
        {
            mainWindow.PresentationModel.WriteIntranetHostFileEntry = (bool)writeIntranetHostCheckbox.IsChecked;
        }

        private void WriteIntranetHostFile_UnChecked(object sender, RoutedEventArgs e)
        {
            mainWindow.PresentationModel.WriteIntranetHostFileEntry = (bool)writeIntranetHostCheckbox.IsChecked;
        }

        private void WritePeopleHostFile_Checked(object sender, RoutedEventArgs e)
        {
            mainWindow.PresentationModel.WritePeopleHostFileEntry = (bool)writePeopleHostCheckbox.IsChecked;
        }

        private void WritePeopleHostFile_UnChecked(object sender, RoutedEventArgs e)
        {
            mainWindow.PresentationModel.WritePeopleHostFileEntry = (bool)writePeopleHostCheckbox.IsChecked;
        }
        
        private void ValidateForm()
        {
            var formIsValid = true;

            if (pbscIntranetHostHeaderForDevTextBox.Text == "")
            {
                formIsValid = false;
            }

            if (pbscIntranetPortForDevTextBox.Text == "")
            {
                formIsValid = false;
            }

            if (pbscPeopleHostHeaderForDevTextBox.Text == "")
            {
                formIsValid = false;
            }

            if (pbscPeoplePortForDevTextBox.Text == "")
            {
                formIsValid = false;
            }

            if (formIsValid)
            {
                mainWindow.nextButton.IsEnabled = true;
            }
            else
            {
                mainWindow.nextButton.IsEnabled = false;
            }
        }

    }
}